package pers.vic.upload.socket.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import pers.vic.upload.socket.model.RealFile;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * 描述: 文件的断点信息
 * @author Vic.xu
 * @date 2022-06-09 10:57
 */
@Service
public class BreakPointService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 对象存储什么暂时还没想好 TODO
     * @param fileIdentity key
     */
    public RealFile getBreakPoint(String fileIdentity) {
        return (RealFile) redisTemplate.opsForValue().get(fileIdentity);
    }

    public void saveBreakPoint(String fileIdentity, RealFile realFile) {
        redisTemplate.opsForValue().set(fileIdentity, realFile, Duration.ofHours(4));
    }

    public void clear(String identity) {
        redisTemplate.expire(identity, 1, TimeUnit.SECONDS);
    }
}
